package com.situ.mall.admin.controller;


import com.situ.mall.admin.service.IAttachmentService;
import com.situ.mall.admin.util.StringUtil;
import com.situ.mall.admin.vo.ResultVO;
import com.situ.mall.admin.vo.UploadAttachmentVO;
import com.situ.mall.common.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author membergpf
 * @since 2023-03-21
 */
@Controller
@RequestMapping("/attachment")
public class AttachmentController {
    @Autowired
    private IAttachmentService attachmentService;

    @PostMapping("/editAttach")
    @ResponseBody
    public UploadAttachmentVO editAttachment(Integer id){

        return attachmentService.selectById(id);
    }
    @PostMapping("/del")
    @ResponseBody
    public ResultVO delete(Integer id){
        ResultVO vo = new ResultVO();
        vo.setData(attachmentService.removeById(id));
        vo.setMsg("删除成功！");
        return vo;
    }
    @PostMapping("/save")
    @ResponseBody
    public ResultVO save(Product product){
        ResultVO vo = new ResultVO();
        attachmentService.add(product.getFirstimg(),product.getId());
        //根据商品id查出附件的所有排序，并获取其中最大值，最大值的下一个值给现在的sequence，如果没有则sequence=1
        int sequence =  attachmentService.selectProId(product.getId());
        if (StringUtil.isNotBlank(product.getImgid())) {
            for (String id : product.getImgid().split(",")) {
                //将前端传来的多张图片的id分隔开
                //将商品的最大ID放入图片id和分隔开的id相等的商品ID里
                sequence++;
                attachmentService.update1(Integer.parseInt(id), product.getId(), product.getName(), sequence);
            }
        }
        return vo;
    }

    @PostMapping("/save2")
    @ResponseBody
    public  ResultVO save2(Integer id,String name){
        System.out.println(id+","+name);
        ResultVO vo = new ResultVO();
        return vo;
    }
}
